/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.HangHoa;

import dao.DanhMucDAO;
import pojo.Danhmuc;
import pojo.Nhanvien;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Quan
 */
public class QLDanhMuc extends javax.swing.JDialog {

    private DefaultTableModel tablemodel;
    private Nhanvien nv;

    /**
     * Creates new form QLDanhMuc
     */
    public QLDanhMuc(Nhanvien nv) {
        this.nv = nv;
        initComponents();
        tablemodel = new DefaultTableModel();
        tablemodel.addColumn("ID");
        tablemodel.addColumn("Tên danh mục");
        jtdanhmuc.setModel(tablemodel);
        LoadCBB();
        LoadData();
    }

    private void LoadCBB() {
        jcb_danhmuccha.removeAllItems();
        jcb_danhmuccha.addItem(null);
        DanhMucDAO dmdao = new DanhMucDAO();
        List dsdm = null;
        try {
            dsdm = dmdao.LayDanhSach();
        } catch (Exception ex) {
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dsdm != null) {
            for (int i = 0; i < dsdm.size(); i++) {
                jcb_danhmuccha.addItem(dsdm.get(i));
            }
        }
    }

    private void LoadData() {
        tablemodel.getDataVector().removeAllElements();
        List<Danhmuc> ds = null;
        DanhMucDAO dmdao = new DanhMucDAO();
        try {
            ds = dmdao.LayDanhSach();
        } catch (Exception ex) {
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (ds != null) {
            for (int i = 0; i < ds.size(); i++) {
                Danhmuc dm = ds.get(i);
                this.tablemodel.addRow(new Object[]{dm.getId(), dm});
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtdanhmuc = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jtf_tendanhmuc = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtf_id = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jcb_danhmuccha = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý danh mục sản phẩm");

        jtdanhmuc.setAutoCreateRowSorter(true);
        jtdanhmuc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtdanhmuc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtdanhmucMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtdanhmuc);

        jButton1.setText("Đóng");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Xóa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Sửa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Thêm mới");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setText("Tên danh mục:");

        jLabel2.setText("Danh mục cha:");

        jtf_id.setEditable(false);

        jLabel3.setText("ID:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtf_id)
                            .addComponent(jtf_tendanhmuc)
                            .addComponent(jcb_danhmuccha, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcb_danhmuccha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_tendanhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        Danhmuc dm = new Danhmuc();
        dm.setTendanhmuc(jtf_tendanhmuc.getText());
        dm.setNgaytao(new Date());
        dm.setNhanvienByNguoitao(this.nv);
        if (jcb_danhmuccha.getSelectedIndex() > 0) {
            dm.setDanhmuc((Danhmuc) jcb_danhmuccha.getSelectedItem());
            dm.setCap(((Danhmuc) jcb_danhmuccha.getSelectedItem()).getCap() + 1);
        } else {
            dm.setCap(1);
        }

        DanhMucDAO dmdao = new DanhMucDAO();
        try {
            dmdao.Them(dm);
            LoadData();
            LoadCBB();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jtdanhmucMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtdanhmucMouseClicked
        // TODO add your handling code here:
        int selected_index = jtdanhmuc.getSelectedRow();
        if (selected_index != -1) {
            jtf_id.setText(jtdanhmuc.getValueAt(selected_index, 0).toString());
            jtf_tendanhmuc.setText(jtdanhmuc.getValueAt(selected_index, 1).toString());

            Long id = ((Danhmuc) tablemodel.getValueAt(selected_index, 1)).getIddanhmuccha();
            if (id == null) {
                jcb_danhmuccha.setSelectedIndex(0);
            } else {
                for (int j = 1; j < jcb_danhmuccha.getItemCount(); j++) {
                    if (((Danhmuc) jcb_danhmuccha.getItemAt(j)).getId() == id) {
                        jcb_danhmuccha.setSelectedIndex(j);
                        break;
                    }
                }

            }
        }
    }//GEN-LAST:event_jtdanhmucMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        DanhMucDAO dmdao = new DanhMucDAO();
        Danhmuc dm = null;
        try {
            dm = (Danhmuc) dmdao.Lay(Long.valueOf(jtf_id.getText()));
        } catch (Exception ex) {
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dm != null) {
            dm.setTendanhmuc(jtf_tendanhmuc.getText());
            if (jcb_danhmuccha.getSelectedIndex() > 0) {
                dm.setDanhmuc((Danhmuc) jcb_danhmuccha.getSelectedItem());
                dm.setCap(((Danhmuc) jcb_danhmuccha.getSelectedItem()).getCap() + 1);
            } else {
                dm.setDanhmuc(null);
                dm.setCap(1);
            }
        }
        try {
            dmdao.Sua(dm);
            LoadCBB();
            LoadData();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        DanhMucDAO dmdao = new DanhMucDAO();
        Danhmuc dm = null;
        try {
            dm = (Danhmuc) dmdao.Lay(Long.valueOf(jtf_id.getText()));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dm != null) {
            try {
                dmdao.Xoa(dm);
                LoadCBB();
                LoadData();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QLDanhMuc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new QLDanhMuc(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcb_danhmuccha;
    private javax.swing.JTable jtdanhmuc;
    private javax.swing.JTextField jtf_id;
    private javax.swing.JTextField jtf_tendanhmuc;
    // End of variables declaration//GEN-END:variables
}
